package com.hbue.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbue.springboot.pojo.DTO.TbHistoryDTO;
import com.hbue.springboot.pojo.Result;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.service.TbHistoryService;
import com.hbue.springboot.utils.CommonPage;
import com.hbue.springboot.vo.QueryParamsVo;
import com.hbue.springboot.vo.WorkerSalaryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/history")
@RequiredArgsConstructor
public class TbHistoryController {

    private final TbHistoryService tbHistoryService;

    /**
     * 查询送水历史——管理员接口
     * 能够按时间段查询，但是由于是单表查询，只能显示userId和workerId
     * @return
     */
    @GetMapping("/list")
    public Result list(TbHistoryDTO condition) {
        Page<TbHistory> page = tbHistoryService.list(condition);
        if (page != null) {
            return Result.success(CommonPage.restPage(page));
        } else {
            return Result.error("没有找到送水历史！");
        }
    }

    /**
     * 查询送水历史——管理员接口——0
     * 能显示userName和workerName，可以按时间段查询
     * @param condition
     * @return
     */
    @GetMapping("/list0")
    public Result listForAdmin(TbHistoryDTO condition) {
        log.info("condition is:"+condition);
        Page<TbHistoryDTO> page = tbHistoryService.listByPage(condition);
        if (page != null) {
            return Result.success(CommonPage.restPage(page));
        } else {
            return Result.error("没有找到送水历史！");
        }
    }

    /**
     * 查询送水历史——送水工接口——1
     * @param condition
     * @return
     */
    @GetMapping("/list1")
    public Result listForWorker(TbHistoryDTO condition) {
        log.info("condition is:"+condition);
        Page<TbHistoryDTO> page = tbHistoryService.listForWorkerByPage(condition);
        if (page != null) {
            return Result.success(CommonPage.restPage(page));
        } else {
            return Result.error("没有找到送水历史！");
        }
    }

    /**
     * 查询送水历史——客户端接口——2
     * @param condition
     * @return
     */
    @GetMapping("/list2")
    public Result listForCust(TbHistoryDTO condition) {
        log.info("condition is:"+condition);
        Page<TbHistoryDTO> page = tbHistoryService.listForCustByPage(condition);
        if (page != null) {
            return Result.success(CommonPage.restPage(page));
        } else {
            return Result.error("没有找到送水历史！");
        }
    }

    /**
     * 查询工资
     * @param
     * @return
     */
    @GetMapping("/listSalary")
    public Result listSalary(@RequestBody QueryParamsVo query) {
        Page<WorkerSalaryVo> page = tbHistoryService.listSalaryByPage(query);
        if (page != null) {
            return Result.success(CommonPage.restPage(page));
        } else {
            return Result.error("没有找到工资！");
        }
    }

    /**
     * 添加送水历史
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody TbHistory history) {
        boolean res = tbHistoryService.save(history);
        if (res) {
            return Result.success(res);
        } else {
            return Result.error("添加送水历史失败！");
        }
    }
    /**
     * 根据id修改送水历史
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody TbHistory tbHistory) {
        boolean res = tbHistoryService.updateById(tbHistory);
        if (res) {
            return Result.success(res);
        } else {
            return Result.error("修改送水历史失败！");
        }
    }

    /**
     * 删除送水历史
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean res = tbHistoryService.removeById(id);
        if (res) {
            return Result.success(res);
        } else {
            return Result.error("删除送水历史失败！");
        }
    }
}
